import threading
import time

from lxml import html

from setting import *
# from tools import *
from util.data_utils import *

push_url = "https://forward.lottefuture.com/api/receive/douban"

def main(key_url,group_name):
    while True:
        print(getCurTime(),f"【{group_name} 开始】")
        
        response = ownRequest(key_url, headers=HEADERS,proxies=PROXIES)
        if not response:
            print(getCurTime(),"获取列表页面失败...")
            # time.sleep(1)
            continue

        tree = html.fromstring(response.text)
        
        # xpath获取所有title标签
        tr_lis = tree.xpath("//*[@id='group-topics']/div[2]/table/tr[position()>1]")
        
        for tr in tr_lis:
            title = tr.xpath("./td[1]/a/text()")[0].strip() if tr.xpath("./td[1]/a/text()") else ""
            url = tr.xpath("./td[1]/a/@href")[0].strip() if tr.xpath("./td[1]/a/@href") else ""
            author = tr.xpath("./td[2]/a/text()")[0].strip() if tr.xpath("./td[2]/a/text()") else ""
            reply = tr.xpath("./td[3]/text()")[0].strip() if tr.xpath("./td[3]/text()") else ""
            time_ = tr.xpath("./td[4]/text()")[0].strip() if tr.xpath("./td[4]/text()") else ""
            
            unique_id = url.split("/")[-2]
            
            # 判断是否推送过
            if R.sismember("unique_set_douban", unique_id):
                #print(f"已经推送过该文章：{article_id}")
                continue
            
            data = {}
            
            #尝试获取文章数据
            try:
                data = get_article_data(url)
            except Exception as e:
                print(getCurTime(),"获取文章数据失败",e)
                continue
            
            # 组装payload
            payload = {
                "title": title,
                "url": url,
                "group_name": group_name,
                "author": author,
                "reply": reply,
                "time": time_,
                "unique_id": unique_id,
            }
            
            # 推送文章数据到远程地址 
            if data and toDatabase(data):
                # 推送此条列表数据到数据库
                response = requests.request("POST", push_url,  data=payload)
                if "接收成功" in response.text:
                    print(getCurTime(),f"该列表URL推送成功 => {url}")
                    # 添加到去重集合 防止重复推送该列表url
                    R.sadd("unique_set_douban",unique_id)
                    # 保存推送时间 为后续更新做准备
                    R.set(unique_id, str(time.time()))
                else:
                    print(getCurTime(),"推送失败",payload)
        print(getCurTime(),f"【{group_name} 结束】")
        # 延时5秒
        time.sleep(5)

if __name__ == "__main__":
    # 买组  拼组
    key_url_lis = [["https://www.douban.com/group/698716/?tab=51757","买组"],["https://www.douban.com/group/536786/?tab=47726","拼组"]]
    # 创建线程列表
    threads = []
    # 创建并启动多个线程
    for key_url in key_url_lis:
        t = threading.Thread(target=main, args=(*key_url,))
        threads.append(t)
        t.start()
    # 等待所有线程完成
    for t in threads:
        t.join()
    # # url  ="https://www.douban.com/group/topic/326622509/?_spm_id=NjE2NTU2OTc"
    # url  ="https://www.douban.com/group/topic/327176140/?_spm_id=MjExNjgwMDM4&_i=9707624EVfAZKY,9707730XjFQJ4B"
    # get_article_data(url)